package com.jm.offer.easy;

/**
 * @Description https://leetcode-cn.com/problems/fan-zhuan-dan-ci-shun-xu-lcof/
 * @date 2022/5/1 7:23
 */
public class _58_I翻转单词顺序 {

    public static void main(String[] args) {
        System.out.println(reverseWords("hello world! "));
    }

    public static String reverseWords(String s) {
        StringBuffer sb = new StringBuffer();
        String[] s1 = s.trim().split(" ");
        for (int i = s1.length - 1; i >= 0; i--) {
            System.out.println(s1[i]);
            if (s1[i].length() != 0){
                sb.append(s1[i]);
                if (i != 0){
                    sb.append(" ");
                }
            }
        }
        return sb.toString();
    }
}
